﻿<!DOCTYPE html>
<html>
<head>
    <title>用户列表</title>$css()
</head>
<body style="margin: 1em;">
<div>
    <span class="ui-button w150" onclick="newRec()">
        <span class="button-inner">
            <span class="button-txt">新增用户</span>
            <a href="javascript:;"></a>
        </span>
    </span>
</div>
<div><br/>
    <div id="dg" class="control" style="border-bottom:solid 1px #ddd"></div>
</div>
<script type="text/template" id="list_menu_tpl">
        <div class="item" onclick="menu.add()">
            <a href="javascript:;">$icon(create)创建新用户</a>
        </div>
        <div class="item" onclick="menu.refresh()">
            <a href="javascript:;">$icon(refresh)刷新</a>
        </div>
    </script>

<script type="text/template" id="tpl">
        <div class="item" onclick="menu.add()">
            <a href="javascript:;">$icon(create)创建新用户</a>
        </div>
        <div class="item" onclick="menu.setavailable('%id%')">
            <a href="javascript:;">$icon(edit)%state%</a>
        </div>
        <div class="item" onclick="menu.del('%id%')">
            <a href="javascript:;">$icon(delete)删除用户</a>
        </div>
        <div class="item" onclick="menu.refresh()">
            <a href="javascript:;">$icon(refresh)刷新</a>
        </div>
    </script>

$js()
<script type="text/javascript">
    jr.dom.fitHeight(jr.$('dg'));
    var _dg = jr.datagrid('dg', {
        url: '?module=user&action=getusers',
        data: { page: 1 },
        idField: 'UserName',
        columns: [
            { field: 'name', title: '姓名', align: 'center', width: 80, formatter: null },
            { field: 'user_name', title: '用户名', align: 'center', width: 100, formatter: null },
            { field: 'id', title: '角色', align: 'center', width: 50, formatter: function(v) {
                    return '<a href="javascript:;" onclick="extRec('+v+')">设置</a>';
                }
            },
            { field: 'phone', title: '电话', align: 'center', width: 100, formatter: null },
            { field: 'email', title: '邮箱', align: 'center', width: 100, formatter: null },
            { field: 'create_time', title: '创建时间', align: 'center', width: 150, formatter: function(v) { return v.replace(/T|\.\d+$/ig, ' '); } },
            { field: 'last_login_time', title: '最近登陆', align: 'center', width: 150, formatter: function (v) { return v.replace(/T|\.\d+$/ig, ' '); } },
            {
                field: 'id',
                title: '最近登陆',
                align: 'center',
                width: 150,
                formatter: function(v, r) {
                    return jr.template('<span class="ui-button w50 small-button" onclick="editRec({id})"><span class="button-inner">'
                        + '<span class="button-txt">修改</span><a href="javascript:;"></a></span></span>&nbsp;&nbsp;' +
                        '<span class="ui-button w50 small-button" onclick="delRec({id})"><span class="button-inner">'
                        + '<span class="button-txt">删除</span><a href="javascript:;"></a></span></span>', {
                            id: v,
                        });
                }
            },
        ],
        loaded: function(data) {
            var tables = document.getElementsByTagName('TABLE');
            jr.table.dynamic(tables[1], false);
        }
    });

    var menu = {
        add: Fn.create,
        setavailable: function(id) {
            (function(_this) {
                jr.xhr.jsonPost('?', 'module=user&action=setuserstate&username=' + id,
                    function(json) {
                        if (json.result) {
                            showMsg2('操作成功！');
                            _this.refresh();
                        } else {
                            jr.dialog.alert(json.message);
                        }
                    });
            }(this));
        },
        refresh: function() {
            _dg.reload();
        }
    };

    function newRec() {
        jr.dialog.create2('新建用户', true, true).open('?module=user&action=newUser',700,450,'auto');
    }
    function editRec(id) {
        jr.dialog.create2('编辑用户', true, true).open('?module=user&action=updateUser&id='+id, 700, 450,'auto');
    }
    function delRec(id) {
        if (confirm('确定删除吗?删除后将不可恢复!')) {
            jr.xhr.jsonPost('?module=user&action=DeleteUser','id=' + id,
                function (json) {
                    if (json.result) {
                        _dg.reload();
                    } else {
                        showErr(json.message);
                    }
                });
        }
    }
    function extRec(id) {
        jr.dialog.create2('用户角色管理', true, true).open('?module=user&action=UserRole&id=' + id, 700, 400,'auto');
    }

</script>
</body>
</html>
